﻿using System;
using System.Linq;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using CIT.WMS.RM.Bll;
using CIT.WMS.Product.Bll;

namespace CIT.WMS.RM
{
    public partial class FrmCenterCode : Form
    {
        public FrmCenterCode()
        {
            InitializeComponent();
        }

        string _BillNo = "";
        decimal _PlanQty = 0;   //计划数量
        string _PartNumber = "";

        private void FrmRMOut_Load(object sender, EventArgs e)
        {
            txtBillNo.Focus();
        }

        /// <summary>
        /// 单号输入事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void txtBillNo_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (e.KeyChar == 13)
            {
                BillNoChange();
            }
        }
        /// <summary>
        /// 库位输入事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void txtLocation_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (e.KeyChar == 13)
            {
                try
                {
                    var arry = txtLocation.Text.Split('#');
                    if (arry.Count() < 3)
                    {
                        MessageBox.Show("输入的库位格式不正确，请重新输入！");
                        txtLocation.Text = "";
                        txtLocation.Focus();
                        return;
                    }

                    var status = Bll_ProductOut.GetLocationSNStatusInInventory(txtLocation.Text, _PartNumber);
                    if (status == 0)
                    {
                        MessageBox.Show("库存不足，选择其他库位！");
                        txtLocation.Text = "";
                        txtLocation.Focus();
                        return;
                    }

                    txtQty.Focus();
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.ToString());
                }
            }
        }

        /// <summary>
        /// 数量输入事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void txtQty_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (e.KeyChar == 13)
            {
                if (string.IsNullOrEmpty(txtLocation.Text))
                {
                    MessageBox.Show("库位不能为空!");
                    txtLocation.Focus();
                    return;
                }

                if (CheckLocation() == false)
                {
                    return;
                }

                decimal qty = 0;
                try
                {
                    qty = Convert.ToDecimal(txtQty.Text);
                }
                catch
                {
                    MessageBox.Show("数量格式错误!");
                    txtQty.Text = "";
                    txtQty.Focus();
                    return;
                }

                if (qty == 0)
                {
                    txtQty.Focus();
                    return;
                }

                var ret = MessageBox.Show("是否确认领用数量为" + txtQty.Text, "确认提示", MessageBoxButtons.OKCancel, MessageBoxIcon.Question, MessageBoxDefaultButton.Button1);
                if (ret == DialogResult.OK)
                {
                    int code = Bll_CenterCode.Query(_BillNo, Utils.uContext.UserNamek__BackingField, txtLocation.Text, qty);
                    if (code == 0)
                    {
                        MessageBox.Show("单号不存在或此单号已处理!");
                    }
                    else if (code == 1)
                    {
                        MessageBox.Show("该库位的库存不足!");
                    }
                    else
                    {
                        var msg = "单号:" + txtBillNo.Text + "\r\n料号:" + _PartNumber + "\r\n计划数量:" + _PlanQty + "\r\n领用数量:" + qty + "\r\n***处理成功***";
                        InitFrm();
                        txtMsg.Text = msg;
                    }
                }
            }
        }

        /// <summary>
        /// 库位效验
        /// </summary>
        private bool CheckLocation()
        {
            try
            {
                var arry = txtLocation.Text.Split('#');
                if (arry.Count() < 3)
                {
                    MessageBox.Show("输入的库位格式不正确，请重新输入！");
                    txtLocation.Text = "";
                    txtLocation.Focus();
                    return false;
                }

                var status = Bll_ProductOut.GetLocationSNStatusInInventory(txtLocation.Text, _PartNumber);
                if (status == 0)
                {
                    MessageBox.Show("库存不足，选择其他库位！");
                    txtLocation.Text = "";
                    txtLocation.Focus();
                    return false;
                }

                //txtQty.Focus();
                return true;
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.ToString());
                return false;
            }
        }
        /// <summary>
        /// 输入单号
        /// </summary>
        private void BillNoChange()
        {
            try
            {
                var tmpBillNo = txtBillNo.Text;
                InitFrm();

                //获取请求物料信息
                var dtRequest = Bll_CenterCode.GetInfo(tmpBillNo);
                if (dtRequest != null && dtRequest.Rows.Count > 0)
                {
                    _PlanQty = Convert.ToDecimal(dtRequest.Rows[0]["Qty"].ToString());
                    _PartNumber = dtRequest.Rows[0]["PartNumber"].ToString();
                    this.txtStorageSN.Text = dtRequest.Rows[0]["Storage_SN"].ToString();
                    txtBillNo.Text = tmpBillNo;
                    _BillNo = tmpBillNo;
                }
                else
                {
                    MessageBox.Show("单号不存在或此单号已处理!");
                    return;
                }

                //消息栏显示
                txtMsg.Text = "单号:" + txtBillNo.Text + "\r\n料号:" + _PartNumber + "\r\n计划数量:" + _PlanQty;

                //获取推荐库位
                var dtLocation = Bll_CenterCode.GetLocation(txtBillNo.Text);
                if (dtLocation != null && dtLocation.Rows.Count > 0)
                {
                    this.txtRcLocation.Text = dtLocation.Rows[0]["Location_SN"].ToString();
                }
                else
                {
                    MessageBox.Show("库存不足,无法推荐库位!");
                    return;
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.ToString());
            }
        }

        /// <summary>
        /// 初始化画面
        /// </summary>
        private void InitFrm()
        {
            _BillNo = "";
            _PlanQty = 0;
            _PartNumber = "";

            txtStorageSN.Text = "";
            txtBillNo.Text = "";
            txtQty.Text = "";
            txtRcLocation.Text = "";
            txtLocation.Text = "";
            txtMsg.Text = "";
            txtBillNo.Focus();
        }



    }
}